<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Constants\QmGame;
use App\Constants\ServerList;
use App\Controller\InsideService\RankInside;
use App\Middleware\AdminMiddleware;
use App\Model\QmGameList;
use App\Service\ExcelService;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: '/admin/Rank'), Middleware(AdminMiddleware::class)]
class RankController extends BaseController {

    /**
     * 获取排行榜
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $userDataArr = $this->uInfo();
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $page = $this->request->query('page', 1);
        $game_hash = $this->request->query('game_hash', '');
        $hero_id = $this->request->query('hero_id', '');
        $type = $this->request->query('type', ''); // pt 爬塔 power 战斗力

        if (empty($game_hash) || empty($hero_id) || empty($type)) {
            return $this->buildSuccess();
        }

        return (new RankInside())->setCode($userDataArr['connection_code'])->index([
            'limit'     => $limit,
            'page'      => $page,
            'game_hash' => $game_hash,
            'hero_id'   => $hero_id,
            'type'      => $type,
        ]);
    }

    /**
     * 描述: 导出排行榜
     * 日期: 2023/4/25 11:01
     * @return array|ResponseInterface
     * @throws Exception
     * @author: cinder
     */
    #[GetMapping(path: 'export')]
    public function export(): array|ResponseInterface {
        set_time_limit(0);
        ini_set('memory_limit', '5048M');
        $userDataArr = $this->uInfo();
        $game_hash = $this->request->query('game_hash', '');
        $hero_id = $this->request->query('hero_id', '');
        $type = $this->request->query('type', ''); // pt 爬塔 power 战斗力

        if (empty($game_hash) || empty($hero_id) || empty($type)) {
            return $this->buildSuccess();
        }
        //导出
        $data = (new RankInside())->setCode($userDataArr['connection_code'])->index([
            'game_hash' => $game_hash,
            'hero_id'   => $hero_id,
            'type'      => $type,
            'page'      => 0 //导出
        ]);

        if($data['data']['count'] <= 0){
            return $this->buildFailed(ErrorCode::INVALID, '1'); //返回标识 前端返回提示
        }

        if ($data['data']['count'] > 1000) {
            return $this->buildFailed(ErrorCode::INVALID, '11'); //返回标识 前端返回提示
        }

        $fileName = '排行榜数据-' . date('YmdHis');
        $title = [
            'ID',
            '所属游戏',
            'openid',
            '用户昵称',
            '贵族等级',
            '角色等级',
            '战斗力',
            '爬塔分数',
        ];
        //表体数据
        $list = $data['data']['list'];
        $data = [];
        foreach ($list as $value) {
            //获取游戏名称
            $data[] = [
                $value['id'],
                QmGame::GAME_LIST[$value['game_hash']],
                $value['openid'],
                $value['nickname'],
                $value['vip'],
                $value['role_level'],
                $value['power'],
                $value['score'],
            ];
        }
        $exportService = new ExcelService();

        //保存临时文件在从浏览器自动下载到本地
        return $exportService->setHeader($title)->addData($data)->saveToBrowserByTmp($fileName);
    }


    /**
     * 将角色踢出排行榜并且重新生成排行榜缓存
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'kickRole')]
    public function kickRole(): array {
        $userDataArr = $this->uInfo();
        $game_hash = $this->request->query('game_hash', '');
        $type = $this->request->query('type', '');
        $role_id = $this->request->query('role_id', '');
        $hero_id = $this->request->query('hero_id', '');

        if (empty($game_hash) || empty($hero_id) || empty($type) || empty($role_id)) {
            return $this->buildFailed(ErrorCode::PARAM_INVALID, '缺少必要参数');
        }

        return (new RankInside())->setCode($userDataArr['connection_code'])->kickRole([
            'game_hash' => $game_hash,
            'type'      => $type,
            'role_id'   => $role_id,
            'hero_id'   => $hero_id,
        ]);
    }
}